package cate.game.role.show.heroskin;


import cate.common.table.d.GDBlog;
import cate.common.table.show.heroskin.row.HeroSkinBaseRow;
import cate.common.table.show.heroskin.row.HeroSkinLevelRow;
import cate.common.table.show.row.*;
import cate.game.GameBody;
import cate.game.attr.FightAttr;
import cate.game.role.Role;
import cate.game.role.show.ShowBagTab;
import cate.game.role.show.avatar.AvatarVO;
import cate.game.role.show.icon.HeadIconVO;
import cate.game.role.show.msg.HeroSkinOnResp;
import cate.game.role.show.msg.ModifyAvatarResp;
import cate.game.role.show.msg.ModifyHeadIconResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.*;


@NoteClass("英雄皮肤")
public class RoleHeroSkin extends ShowBagTab<HeroSkin> {

    @NoteField("图鉴")
    public HeroSkinIllustrate illustrate;

    @NoteField("商城")
    public HeroSkinMall mall;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (illustrate == null) {
            illustrate = new HeroSkinIllustrate();
        }
        illustrate.initialize(role);
        if (mall == null) {
            mall = new HeroSkinMall();
        }
        mall.initialize(role);
    }

    @Override
    public void onEnter() {
        mall.onEnter();
    }

    @Override
    public byte getType() {
        return GDBlog.DecorateType.HERO_SKIN;
    }

    @Override
    protected HeroSkin createItem(int tid, long validTime) {
        return new HeroSkin(tid,validTime);
    }

    @Override
    public void onInvalid(List<Integer> expireTids) {
        for (Integer skinId : expireTids) {
            // 脱掉英雄穿着的皮肤
            for (Map.Entry<Integer, Integer> entry : illustrate.heroSkin.entrySet()) {
                if (Objects.equals(entry.getValue(), skinId)) {
                    entry.setValue(0);
                    role.sendNow(new HeroSkinOnResp(entry.getKey(), 0));
                }
            }
            // 判断是否需要重置头像
            HeadIconVO headIconVO = role.getBase().headIcon;
            HeadIconRow headIconRow = role.getGame().table.blog.headIcon.get(headIconVO.tid);
            if (headIconRow != null && headIconRow.activeType == GDBlog.ActiveType.HERO && headIconRow.value == skinId) {
                headIconVO.tid = role.getShow().headIcon.findDefaultTid();
                role.sendNow(new ModifyHeadIconResp(role));
            }
            // 判断是否需要重置冒险形象
            AvatarVO avatarVO = role.getBase().avatar;
            AvartarRow avatarRow = role.getGame().table.blog.avartar.get(avatarVO.tid);
            if (avatarRow != null && avatarRow.activeType == GDBlog.ActiveType.HERO && avatarRow.value == skinId) {
                avatarVO.tid = role.getShow().avartar.findDefaultTid();
                role.sendNow(new ModifyAvatarResp(role));
            }
        }
    }

    public FightAttr getIllustrateAttr(GameBody gameBody) {
        FightAttr attr = new FightAttr();
        //皮肤属性
        List<HeroSkin> list = new ArrayList<>(this.list);
        int level = list.size() + list.stream().mapToInt(e -> e.getLevelRow(gameBody).score).sum();
        List<String> attrs = gameBody.table.heroSkin.score.getLevelRow(level);
        attrs.forEach(e -> attr.add(new FightAttr(e)));
        return attr;
    }

    @JsonIgnore
    public Map<Integer, FightAttr> getAttrMap() {
        Map<Integer, FightAttr> attrMap = new HashMap<>();
        //皮肤属性
        List<HeroSkin> list = new ArrayList<>(this.list);
        for (HeroSkin skin : list) {
            HeroSkinBaseRow baseRow = skin.getBaseRow(role.getGame());
            if (baseRow != null) {
                HeroSkinLevelRow levelRow = skin.getLevelRow(role.getGame());
                if (levelRow != null) {
                    FightAttr attr = attrMap.get(baseRow.protoId);
                    if (attr == null) {
                        attr = new FightAttr();
                        attrMap.put(baseRow.protoId, attr);
                    }
                    attr.add(new FightAttr(levelRow.attrStr));
                }
            }
        }
        return attrMap;
    }
}
